package com.gears_software.sxdc.dccx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.model.PositionEntity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/9/2.
 */

@EActivity(R.layout.activity_jiesongji)
public class JieSongJiActivity extends AppCompatActivity {
    @App
    DCCXApplication app;
    @ViewById
    TextView back_group_icon;
    @ViewById
    RadioGroup group;
    @ViewById
    RadioButton radio_jieji;
    @ViewById
    RadioButton radio_songji;
    @ViewById
    TextView txt_chengcheren;
    @ViewById
    TextView txt_hangban;
    @ViewById
    TextView txt_shang_time;
    @ViewById
    TextView txt_shang_didian;
    @ViewById
    TextView txt_xia_didian;
    @ViewById
    TextView txt_shangche_address;
    @ViewById
    TextView txt_xiache_address;
    @ViewById
    TextView txt_chufa_time;
    @ViewById
    RelativeLayout re_hangban;
    @ViewById
    RelativeLayout re_end;
    @ViewById
    RelativeLayout re_start;
    @ViewById
    RelativeLayout re_time;
    @ViewById
    View line;
    @Extra
    String currentCity;

    List<String> options1Items = new ArrayList<String>();
    OptionsPickerView pickerView;
    int checkedid;
    TimePickerView pvTime;
    private boolean[] type = new boolean[]{false, true, true, true, true, false};//显示类型 默认全部显示

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        checkedid = radio_jieji.getId();
        options1Items.add("太原武宿国际机场");
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                checkedid = checkedId;
                if (checkedId == radio_jieji.getId()) {
                    re_hangban.setVisibility(View.VISIBLE);
                    line.setVisibility(View.VISIBLE);
                    txt_shangche_address.setText("机场");
                    txt_xiache_address.setText("");
                    txt_xiache_address.setHint("您在那儿下车");

                } else if (checkedId == radio_songji.getId()) {
                    re_hangban.setVisibility(View.GONE);
                    line.setVisibility(View.GONE);
                    txt_shangche_address.setText("");
                    txt_shangche_address.setHint("您在那儿上车");
                    txt_xiache_address.setText("机场");

                }
            }
        });

        pickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (checkedid == radio_jieji.getId()) {
                    txt_shangche_address.setText(options1Items.get(options1));
                } else if (checkedid == radio_songji.getId()) {
                    txt_xiache_address.setText(options1Items.get(options1));
                }
            }
        }).build();

        pickerView.setPicker(options1Items);

        initTimePicker();
    }

    void initIconFont() {
        back_group_icon.setTypeface(app.iconfont);
        txt_chengcheren.setTypeface(app.iconfont);
        txt_hangban.setTypeface(app.iconfont);
        txt_shang_time.setTypeface(app.iconfont);
        txt_shang_didian.setTypeface(app.iconfont);
        txt_xia_didian.setTypeface(app.iconfont);
    }

    void initTimePicker() {
        Calendar startDate = Calendar.getInstance();
        startDate.set(startDate.get(Calendar.YEAR), startDate.get(Calendar.MONTH), startDate.get(Calendar.DAY_OF_MONTH));
        Calendar endDate = Calendar.getInstance();
        endDate.set(endDate.get(Calendar.YEAR), endDate.get(Calendar.MONTH), endDate.get(Calendar.DAY_OF_MONTH) + 3);
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                txt_chufa_time.setText(getTime(date));
            }
        })
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.GRAY)//取消按钮文字颜色
                .isCyclic(false)//是否循环滚动
                .setType(type)
                .setDate(Calendar.getInstance())// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .setLabel("年", "月", "日", "", "", "")
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    @Click
    void re_time() {
        closeKeyboard();
        pvTime.show();
    }

    @Click
    void re_start() {
        if (checkedid == radio_jieji.getId()) {
            pickerView.show();
        } else if (checkedid == radio_songji.getId()) {
            Intent intent = new Intent(this, DestinationActivity_.class);
            intent.putExtra("title", "选择上车地点");
            intent.putExtra("flag", "start");
            intent.putExtra("currentCity", currentCity);
            startActivityForResult(intent, 1);

        }
    }

    @Click
    void re_end() {
        if (checkedid == radio_jieji.getId()) {
            Intent intent = new Intent(this, DestinationActivity_.class);
            intent.putExtra("title", "选择下车地点");
            intent.putExtra("flag", "end");
            intent.putExtra("currentCity", currentCity);
            startActivityForResult(intent, 1);
        } else if (checkedid == radio_songji.getId()) {
            pickerView.show();

        }
    }

    @OnActivityResult(value = 1)
    void showResult(Intent intent) {
        if (intent != null) {
            PositionEntity entity = (PositionEntity) intent.getExtras().getSerializable("entity");
            if (entity != null) {
                if (entity.getName() != null && !entity.getName().equals("")) {
                    if (checkedid == radio_jieji.getId()) {
                        txt_xiache_address.setText(entity.getName());
                    } else if (checkedid == radio_songji.getId()) {
                        txt_shangche_address.setText(entity.getName());
                    }
                }
            }
        }
    }

    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }


    @Click
    void back_group_icon() {
        this.finish();
    }
}
